package com.ly.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ly.mapper.ArticleMapper;
import com.ly.pojo.Article;
import com.ly.pojo.PageBean;
import com.ly.service.ArticleServeice;
import com.ly.utils.ThreadLocalUtil;

@Service
public class ArticleServeiceinmpl implements ArticleServeice{

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void add(Article article) {
        // 补充属性值
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map =  ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
       //创建PageBean对象
       PageBean<Article> pb = new PageBean<>();

        // 开启分页查询 pagehelper
        PageHelper.startPage(pageNum,pageSize);

        // 调用mapper
        Map<String,Object> map =  ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Article> as = articleMapper.list(userId,categoryId,state);
        Page<Article> p = (Page<Article>) as;

        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.update(article);
    }

    @Override
    public Article findById(Integer id) {
        Article a = articleMapper.findById(id);
        return a;
    }

    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }

}   
